﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_CompleteTransit : AdminBasePage
{
    BLLTrackPackage bllTrackPackage = new BLLTrackPackage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindPackages();
        }
    }

    protected void txtBarcode_TextChanged(object sender, EventArgs e)
    {
        BLLPackageManagement bllPackage = new BLLPackageManagement();

        string barcode = txtBarcode.Text.Replace("+","").Trim();
        if (!string.IsNullOrEmpty(barcode))
        {
            tblPackage package = bllPackage.GetPackage(barcode);
            if (package != null)
            {
                tblTransit transit = bllPackage.GetLastTransit(package.ID);
                if (transit != null)
                {
                    if (transit.BranchID != LoginedStaff.BranchID)
                    {
                        lblError.Text = "Sorry, you do not allowed to complete this package. ";
                        lblError.Visible = true;
                        return;
                    }
                    package.Status = bllPackage.GetStatus("Completed").ID;
                    bllPackage.UpdatePackage();
                    bindPackages(package.Barcode);
                }
            }
            else
            {

            }
            txtBarcode.Text = "";
        }

        Page.DataBind();
    }


    private void bindPackages()
    {

    }

    private void bindPackages(string barcode)
    {
        dlPackage.DataSource = new List<vPackageDetail>() { bllTrackPackage.GetvPackage(barcode) };
        dlPackage.DataBind();
    }


    protected void dlPackage_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        vPackageDetail detail = e.Item.DataItem as vPackageDetail;
        if (detail != null)
        {
            DataList dl = e.Item.FindControl("dlTransit") as DataList;
            if (dl != null)
            {
                dl.DataSource = bllTrackPackage.GetvPackageTransits(detail.ID);
                dl.DataBind();
            }
        }
    }

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        txtBarcode_TextChanged(sender, e);
    }
}